package com.tms.app.tms.ui;

import com.tms.app.tms.app.Constant;
import com.tms.app.tms.app.MyApp;
import com.tms.app.tms.bean.GpsBean;
import com.tms.app.tms.bean.response.BaseResponse;
import com.tms.app.tms.bean.response.NodeTaskRes;
import com.tms.app.tms.ui.base.presenter.AbsServicexUtils;
import com.tms.app.tms.ui.base.presenter.BaseListPresenter;
import com.tms.app.tms.ui.base.presenter.BasePresenter;
import com.tms.app.tms.utils.ImageUtil;
import com.tms.app.tms.utils.SharePreferenceUtil;

import org.xutils.DbManager;
import org.xutils.common.Callback;
import org.xutils.common.util.KeyValue;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.ex.DbException;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.util.List;

import cn.edu.zafu.camera.BdJson;
import cn.edu.zafu.camera.OrcText;
import cn.edu.zafu.camera.OrcTextUtil;

import static com.tms.app.tms.app.Constant.BD_token;

/**
 * Created by Administrator on 2017/12/1 0001.
 */

public class NodeTaskeP extends BaseListPresenter{
    private String afterBase64;

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        if (requestType==1){// 获取node任务
            NodeTaskRes nodeTaskRes=gson.fromJson(result,NodeTaskRes.class);
            sendMsgToView(nodeTaskRes);
        }
        if (requestType==2){//完成node任务
            sendMsgToView(result);
        }
//        if (requestType==2017){
//            try {
//                DbManager db = x.getDb(MyApp.getDaoConfig());
//                if (db == null)
//                    return;
//                db.delete(GpsBean.class, WhereBuilder.b("status","=",0));
//               //db.delete(GpsBean.class, WhereBuilder.b("status","=",0),new KeyValue("status",1));
//            } catch (DbException e) {
//                e.printStackTrace();
//            }
//        }

    }


    @Override
    public void onFinished() {
        super.onFinished();
    }

    /*****
     * 获取node任务
     * @param token
     * @param stationExecId
     */
    public void   getNodeTask(String token, int  stationExecId){
      requestType=1;
      RequestParams params=new RequestParams(Constant.station_detail);
      params.addBodyParameter("token",token);
      params.addBodyParameter("stationExecId",stationExecId+"");
      params.addBodyParameter("showLoading", AbsServicexUtils.showAndHideLoading);
      getObjectFromServer(params,1);
  }

    /*******
     * 完成node任务
     * @param token
     * @param stationExecId
     * @param containerNo
     * @param sealNo
     * @param files
     * @param imgPath
     */
  public void stationDo(String token,int stationExecId, String containerNo, String sealNo , List<String> files,String imgPath,String tare) {
      requestType=2;
      RequestParams params=new RequestParams(Constant.station_do);
      params.addBodyParameter("token",token);
      params.addBodyParameter("containerNo",containerNo);
      params.addBodyParameter("sealNo",sealNo);
      params.addBodyParameter("stationExecId",stationExecId+"");
      params.addBodyParameter("tare",tare+"");
      params.setMultipart(true);
      if (files!=null&&files.size()>0){
          for (int i = 0; i < files.size(); i++) {
              params.addBodyParameter("files",new File(files.get(i)));
          }
      }
      params.addBodyParameter("imgs",imgPath);
      params.addBodyParameter("showLoading", AbsServicexUtils.showAndHideLoading);
      getObjectFromServer(params,1);
  }

    public void uploadAndRecognize(int orcType,String imgPath) {
        requestType=3;
        String BDToken=  SharePreferenceUtil.getString(MyApp.getInstance().getApplicationContext(),"BDToken",BD_token);
        RequestParams params = null;
        if (orcType == 1)
            params = new RequestParams("https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic");//普通
        else//高清
            params = new RequestParams("https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic");

        params.addHeader("Content-Type", "application/x-www-form-urlencoded");
        params.setMultipart(true);
        params.addBodyParameter("access_token",BDToken );
        afterBase64 = ImageUtil.bitmapToString(imgPath);
        params.addBodyParameter("image", afterBase64);
        params.addBodyParameter("detect_direction", "true");
        params.addBodyParameter("language_type", "ENG");
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                sendMsgToView(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                    ex.printStackTrace();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void dd(){
//
//            System.out.println(result);
//            BdJson json = gson.fromJson(result, BdJson.class);
//            if (json == null) {
//                handler.sendEmptyMessage(-1);
//                resultCode = 0;
//                resultDb = result;
//                return;
//            }
//            if (json.getWords_result() == null || json.getWords_result().size() < 1) {
//                handler.sendEmptyMessage(-1);
//                resultCode = 0;
//                resultDb = result;
//                return;
//            }
//            resultDb = json.getWords_result().get(0).getWords();
//            resultDb = OrcTextUtil.formatOrcText(resultDb);
//            if (OrcTextUtil.checkTextRight(resultDb))
//                resultCode = 1;
//            else
//                resultCode = 0;
//            handler.sendEmptyMessage(222);


    }

}
